# -*- coding: utf-8 -*-

import sqlite3

from . import config


def init_datebase():
    with sqlite3.connect(config.DATEBASE) as db:
        cur = db.cursor()
        cur.execute(
            """CREATE TABLE IF NOT EXISTS `users` (`uid`   INTEGER PRIMARY KEY,
                                                   `uname` TEXT,
                                                   `pwd`   TEXT,
                                                   `name`  TEXT,
                                                   `group` INTEGER);""")
        cur.execute(
            """CREATE TABLE IF NOT EXISTS `articles` (`aid`         INTEGER PRIMARY KEY,
                                                      `title`       TEXT,
                                                      `content`     TEXT,
                                                      `author_id`   INTEGER,
                                                      `author_name` TEXT,
                                                      `last_time`   INTEGER);""")
        cur.execute(
            """CREATE TABLE IF NOT EXISTS `replies` (`rid`         INTEGER PRIMARY KEY,
                                                     `aid`         INTEGER,
                                                     `content`     TEXT,
                                                     `author_id`   INTEGER,
                                                     `author_name` TEXT,
                                                     `time`   INTEGER);""")
        cur.execute(
          """CREATE TABLE IF NOT EXISTS `msgs` (`mid`       INTEGER PRIMARY KEY,
                                                `from_id`   INTEGER,
                                                `from_name` TEXT,
                                                `to_id`     INTEGER,
                                                `to_name`   TEXT,
                                                `content`   TEXT,
                                                `time`      INTEGER);""")
